package com.pinyougou.sellergoods.service.impl;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.pojo.TbBrandExample;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;

@Service
public class BrandServiceImpl implements BrandService {

	@Autowired
	private TbBrandMapper brandMapper;

	/**
	 * 分页查询所有品牌
	 * @param currentPage
	 * @param size
	 * @return
	 */
	@Override
	public PageInfo findAll(Integer currentPage, Integer size) {

		PageHelper.startPage(currentPage,size);

		List<TbBrand> tbBrands = brandMapper.selectByExample(null);

		return new PageInfo(tbBrands);
	}

	/**
	 * 添加
	 * @param brand
	 */
	@Override
	public void add(TbBrand brand) {
		brandMapper.insert(brand);
	}

	/**
	 * 根据id查询一个
	 * @param id
	 * @return
	 */
	@Override
	public TbBrand findOne(Long id) {
		return brandMapper.selectByPrimaryKey(id);
	}

	/**
	 * 更新
	 * @param brand
	 */
	@Override
	public void update(TbBrand brand) {
		brandMapper.updateByPrimaryKey(brand);
	}

	/**
	 * 批量删除
	 * @param ids
	 */
	@Override
	public void delete(Long[] ids) {
		for (Long id : ids) {
			brandMapper.deleteByPrimaryKey(id);
		}
	}

	/**
	 * 根据搜索框条件查询
	 * @param brand
	 * @param currentPage
	 * @param size
	 * @return
	 */
	@Override
	public PageInfo findAll(TbBrand brand, Integer currentPage, Integer size) {
		PageHelper.startPage(currentPage,size);//分页

        //创建查询条件
		TbBrandExample brandExample = new TbBrandExample();
		TbBrandExample.Criteria criteria = brandExample.createCriteria();

		if (brand != null){
		    //按名称查询
			if(brand.getName()!=null && brand.getName().length()>0){
				criteria.andNameLike("%"+brand.getName()+"%");
			}
			//按首字母查询
			if(brand.getFirstChar()!=null && brand.getFirstChar().length()>0){
				criteria.andFirstCharEqualTo(brand.getFirstChar());
			}
		}
		List<TbBrand> tbBrands = brandMapper.selectByExample(brandExample);

		return new PageInfo(tbBrands);

	}

	/**
	 * 品牌下拉框数据
	 * @return
	 */
	@Override
	public List<Map> selectOptionList() {
		return brandMapper.selectOptionList();
	}


}
